package com.xwyzsn.backend.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xwyzsn.backend.job.common.Result;
import com.xwyzsn.backend.job.entity.Project;
import com.xwyzsn.backend.job.entity.ProjectDetail;
import com.xwyzsn.backend.job.entity.Teacher;
import com.xwyzsn.backend.job.mapper.ProjectMapper;
import com.xwyzsn.backend.job.mapper.ProjectStudentUnion;
import com.xwyzsn.backend.job.mapper.TeacherMapper;
import com.xwyzsn.backend.job.service.ProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import static java.time.LocalTime.now;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-03
 */

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {


    @Autowired
    private ProjectStudentUnion projectStudentUnionMapper;

    public Result confirm(String snum, String pid) {
        UpdateWrapper<Project> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("pid", pid);
        updateWrapper.set("snum", snum);
        updateWrapper.set("status", 1);
        int res = projectMapper.update(null, updateWrapper);
        if (res == 0) {
            return Result.fail("confirm failed");
        }
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        Project project = projectMapper.selectOne(queryWrapper);

        return Result.success(project);
    }

    public Result cancel(String snum, String pid) {
        UpdateWrapper<Project> projectUpdateWrapper = new UpdateWrapper<>();
        projectUpdateWrapper.eq("pid", pid);
        projectUpdateWrapper.set("snum", null);
        projectUpdateWrapper.set("status", 0);
        int res = projectMapper.update(null, projectUpdateWrapper);
        if (res == 0) {
            return Result.fail("cancel failed");
        } else {
            return Result.success("cancel success");
        }

    }

    public Result update(String pid, String pname, String pdesc) {
        UpdateWrapper<Project> projectUpdateWrapper = new UpdateWrapper<>();
        projectUpdateWrapper.eq("pid", pid);
        projectUpdateWrapper.set("pname", pname);
        projectUpdateWrapper.set("pdesc", pdesc);
        projectUpdateWrapper.set("created_time", new Date());
        int res = projectMapper.update(null, projectUpdateWrapper);
        if (res == 0) {
            return Result.fail("update failed");
        } else {
            return Result.success("update success");
        }
    }

    public Result add(String pname, String pdesc, String dept, String tnum) {
        Project project = new Project(tnum, pname, pdesc, new Date(), new Date(), 0, dept);
        int res = projectMapper.insert(project);
        if (res == 0) {
            return Result.fail("add failed");
        } else {
            return Result.success("add success");
        }
    }

    public Result delete(String pid) {
        int res = projectMapper.deleteById(pid);
        if (res == 0) {
            return Result.fail("delete failed");
        } else {
            return Result.success("delete success");
        }
    }

    public Result getStudentStatus(String dept) {
        QueryWrapper<ProjectStudentUnion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p.dept", dept);
        queryWrapper.eq("status", 1);
        List<ProjectStudentUnion> projects = projectStudentUnionMapper.selectUnionByDept(queryWrapper);
        return Result.success(projects);

    }

    public Result getStudentNotStatus(String dept) {

        QueryWrapper<ProjectStudentUnion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p.dept", dept);
        queryWrapper.eq("status", 0);
        List<ProjectStudentUnion> projects = projectStudentUnionMapper.selectUnionByDept(queryWrapper);
        return Result.success(projects);
    }

    public Result getSelectedProject(String dept) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept", dept);
        queryWrapper.eq("status", 1);
        List<Project> projects = projectMapper.selectList(queryWrapper);
        return Result.success(projects);


    }

    public Result getNotSelectedProject(String dept) {

        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept", dept);
        queryWrapper.eq("status", 1);
        List<Project> projects = projectMapper.selectList(queryWrapper);

        return Result.success(projects);
    }

    private class ProjectStatistics {
        private Integer total;
        private Integer avail;

        public ProjectStatistics(Integer total, Integer avail) {
            this.total = total;
            this.avail = avail;
        }

        public Integer getTotal() {
            return total;
        }

        public void setTotal(Integer total) {
            this.total = total;
        }

        public Integer getAvail() {
            return avail;
        }

        public void setAvail(Integer avail) {
            this.avail = avail;
        }
    }

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private TeacherMapper teacherMapper;


    public Result getStatistics() {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        Integer total = projectMapper.selectCount(null);
        queryWrapper.eq("status", 1);
        Integer avail = projectMapper.selectCount(queryWrapper);
        ProjectStatistics projectStatistics = new ProjectStatistics(total, avail);
        return Result.success(projectStatistics);
    }

    public Result getRecent(String dept) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("created_time");
        queryWrapper.eq("dept", dept);
        queryWrapper.last("limit 10");
        return Result.success(projectMapper.selectList(queryWrapper));
    }

    public Result getAll(String dept) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("created_time");
        queryWrapper.eq("dept", dept);
        return Result.success(projectMapper.selectList(queryWrapper));
    }

    public Result check(String snum) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("snum", snum);
        Project project = projectMapper.selectOne(queryWrapper);
        if (project == null) {
            return Result.fail("not found");
        }
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("tnum", project.getTnum());
        
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        ProjectDetail projectDetail = new ProjectDetail(project, teacher.getTname(), teacher.getTtile()
                , teacher.getLocation(), teacher.getTel(), teacher.getEmail(), teacher.getQq());
        return Result.success(projectDetail);
    }
}
